package com.qf.agro.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.CustomException;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.SysUserMapper;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysUserService;
import com.qf.agro.util.JwtUtil;
import com.qf.agro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/5/22 10:30
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    private final SysUserMapper sysUserMapper;

    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public ResultInfo login(String username, String password) {
        //1.严格的参数校验
        if(StringUtils.isEmpty(username))
        {
            throw new RuntimeException("用户名不能为空");
        }
        if(StringUtils.isEmpty(password))
        {
            throw new RuntimeException("登录密码不能为空");
        }

       //  ②. 调用mapper层.根据用户名称查询出用户信息.
        //1-1 根据用户名称,查询出相关的数据.
        LambdaQueryWrapper<SysUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUsername,username);
        List<SysUser> sysUsers = sysUserMapper.selectList(lqw);

           /*  1-2 判断一下查询结果返回的集合元素个数是不是为0.
 0: 根据用户名称并没有查询到相关的用户信息. ---> 登录失败了.用户错
                了.有1个 --> 正经人应该有的数据.多个, 多个抛出异常.*/
        if(sysUsers.size()==0){
           throw new CustomException("用户名不存在");
        }
        if(sysUsers.size()>1){
            throw new RuntimeException("系统出现异常");
        }

        // ③. 取出集合当中查询出来的数据.SysUser对象.
        SysUser sysUser = sysUsers.get(0);

        String md5Password = SecureUtil.md5(password);

        String userPassword = sysUser.getPassword();

        // ④. 验证密码的正确性
        if(!md5Password.equals(userPassword)){
            return ResultInfo.error("用户名或者密码出错啦");
        }
        // ⑤. 开始生成token了.
        String subject= JSON.toJSONString(sysUser);
        String token = JwtUtil.createJWT(sysUser.getId().toString(), subject, null);


        return ResultInfo.success(token);
    }

    @Override
    public ResultInfo getPage(Long pageNum, Long pageSize, String name) {
        LambdaQueryWrapper<SysUser> lqw=new LambdaQueryWrapper<>();
        lqw.like(org.apache.commons.lang3.StringUtils.isNotEmpty(name),SysUser::getUsername,name);
        lqw.eq(SysUser::getState,1);
        Page<SysUser> page=new Page<>(pageNum,pageSize);

        page = sysUserMapper.selectPage(page, lqw);


        return ResultInfo.success(page);
    }

    @Override
    public ResultInfo deleteUser(Integer userId) {
        LambdaQueryWrapper<SysUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getId,userId);
        SysUser sysUser = sysUserMapper.selectOne(lqw);
        sysUser.setState(0);
        sysUserMapper.update(sysUser,lqw);

        return ResultInfo.success("修改成功");
    }

    @Override
    public ResultInfo updateUser(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        return ResultInfo.success(sysUser);
    }

    @Override
    public ResultInfo checkUsername(String username) {
        LambdaQueryWrapper<SysUser> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUsername,username);
        List<SysUser> sysUsers = sysUserMapper.selectList(lqw);

        if(sysUsers!=null&&sysUsers.size()>0)
        {
            return ResultInfo.error("当前用户已存在");
        }
        return ResultInfo.success("当前用户名可用");
    }

    @Override
    public ResultInfo insertUser(SysUser sysUser) {

        sysUserMapper.insert(sysUser);
        return ResultInfo.success("添加成功");
    }
}
